package com.chainshop.product.controller;

import com.chainshop.common.annotation.Log;
import com.chainshop.common.core.controller.BaseController;
import com.chainshop.common.core.domain.AjaxResult;
import com.chainshop.common.core.page.TableDataInfo;
import com.chainshop.common.enums.BusinessType;
import com.chainshop.common.utils.poi.ExcelUtil;
import com.chainshop.common.core.domain.pojo.ItemInformation;
import com.chainshop.product.domain.vo.AddProductVo;
import com.chainshop.product.service.IItemInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品信息Controller
 * 
 * @author chainshop
 * @date 2024-08-07
 */
@RestController
@RequestMapping("/Product/product")
public class ItemInformationController extends BaseController
{
    @Autowired
    private IItemInformationService itemInformationService;
    @GetMapping("/list1")
    public AjaxResult list1(AddProductVo productVo)
    {
        List<AddProductVo> list = itemInformationService.selectItemInformationsList(productVo);
        return AjaxResult.success(list);
    }

    /**
     * 查询商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('Product:product:list')")
    @GetMapping("/list")
    public TableDataInfo list(AddProductVo productVo)
    {
        startPage();
        List<AddProductVo> list = itemInformationService.selectItemInformationsList(productVo);
        return getDataTable(list);
    }

    /**
     * 导出商品信息列表
     */
    @PreAuthorize("@ss.hasPermi('Product:product:export')")
    @Log(title = "商品信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ItemInformation itemInformation)
    {
        List<ItemInformation> list = itemInformationService.selectItemInformationList(itemInformation);
        ExcelUtil<ItemInformation> util = new ExcelUtil<ItemInformation>(ItemInformation.class);
        util.exportExcel(response, list, "商品信息数据");
    }

    /**
     * 获取商品信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('Product:product:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(itemInformationService.selectItemInformationById(id));
    }

    /**
     * 新增商品信息
     */
    @PreAuthorize("@ss.hasPermi('Product:product:add')")
    @Log(title = "商品信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AddProductVo productVo)
    {

        return AjaxResult.success(itemInformationService.insertItemInformation(productVo));
    }

    /**
     * 修改商品信息
     */
    @PreAuthorize("@ss.hasPermi('Product:product:edit')")
    @Log(title = "商品信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AddProductVo productVo)
    {
        return toAjax(itemInformationService.updateItemInformation(productVo));
    }

    /**
     * 删除商品信息
     */
    @PreAuthorize("@ss.hasPermi('Product:product:remove')")
    @Log(title = "商品信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(itemInformationService.deleteItemInformationByIds(ids));
    }
    @GetMapping("/getProductUnit")
    public AjaxResult getProductUnit()
    {
        return success(itemInformationService.getProductUnit());
    }
    @GetMapping("/getProductAttribute/{id}")
    public AjaxResult getProductAttribute(@PathVariable Long id)
    {
        System.out.println(itemInformationService.getProductAttribute(id));
        return success(itemInformationService.getProductAttribute(id));
    }
}
